package com.atguigu.edu.controller;


import com.atguigu.edu.entity.EduCourse;
import com.atguigu.edu.service.EduCourseService;
import com.atguigu.request.CourseCondition;
import com.atguigu.request.CourseInfoVO;
import com.atguigu.response.EduCourseConfirmVO;
import com.atguigu.response.RetVal;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author HB
 * @since 2020-11-09
 */
@RestController
@RequestMapping("/edu/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     * 添加课程
     *
     * @param courseInfoVO
     * @return
     */
    @PostMapping("saveCourseInfo")
    public RetVal saveCourseInfo(CourseInfoVO courseInfoVO) {
        String courseId = eduCourseService.saveCourseInfo(courseInfoVO);
        return RetVal.success().data("courseId",courseId);
    }

    /**
     * 分页查询
     * 分页查询的步骤：
     * 1.定义两个long型分页参数：pageNum pageSize
     * 2. 再service中实现条件查询
     *
     * @return
     */
    @GetMapping("getCoursePageCondition/{pageNum}/{pageSize}")
    public RetVal getCoursePageCondition(@PathVariable("pageNum") long pageNum, @PathVariable("pageSize") long pageSize, CourseCondition courseCondition) {
        //根据课程这个数据库进行分页查询
        Page<EduCourse> page = new Page<>(pageNum, pageSize);
        eduCourseService.getCoursePageCondition(page, courseCondition);
        long total = page.getTotal();
        List<EduCourse> courseList = page.getRecords();
        return RetVal.success().data("total", total).data("courseList", courseList);
    }

    /**
     * 根据课程id查询课程的信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public RetVal queryCourseById(@PathVariable("id") String id){
        CourseInfoVO courseInfoVO=eduCourseService.queryCourseById(id);
            return RetVal.success().data("courseInfoVO",courseInfoVO);

    }

    /**
     * 更新课程信息
     * @param courseInfoVO
     * @return
     */
    @PutMapping("updateCourseInfo")
    public RetVal updateCourseInfo(CourseInfoVO courseInfoVO) {
        eduCourseService.updateCourseInfo(courseInfoVO);
        return RetVal.success();
    }


    /**
     * 课程的发布确认消息
     * @param courseId
     * @return
     */
    @GetMapping("coursePublishConfirmInfo/{courseId}")
    public RetVal coursePublishConfirmInfo(@PathVariable("courseId") String courseId) {
        EduCourseConfirmVO courseConfirmVO=eduCourseService.coursePublishConfirmInfo(courseId);
        return RetVal.success().data("courseConfirmVO",courseConfirmVO);
    }

    //课程发布状态 修改course表的status即可
    @GetMapping("publishCourse/{courseId}")
    public RetVal publishCourse(@PathVariable("courseId") String courseId) {
        EduCourse course = new EduCourse();
        course.setId(courseId);
        course.setStatus("Normal");
        eduCourseService.updateById(course);
        return RetVal.success();
    }

    /**
     * 根据课程id 删除课程的信息
     * @param courseId
     * @return
     */
    @DeleteMapping("{courseId}")
    public RetVal deleteCourseById(@PathVariable("courseId") String courseId) {
        eduCourseService.deleteCourseById(courseId);
        return RetVal.success();
    }


}

